/*
 * @Description: model description...
 * @Author: pinus
 * @Email: pinus0716@163.com
 * @Git: https://gitee.com/xyf97
 * @Date: 2021-10-29 19:22:32
 * @LastEditTime: 2021-10-30 13:47:27
 */
// pages/song/song.js
import request from '../../utils/request.js'
const moment = require('moment')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    index: 0, // 这首歌在歌单中的index
    url: '',
    isPlay: false,
    songInfo: {},
    songsSheet: [],
    isSheetShow: false,
    proc: 0,
    currenTime: '00:00',
    durationTime: '00:00',
    playMode: 'list' // 列表循环
  },
  backgroundAudioManager: null,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    const thisSongInfo = JSON.parse(options.data)
    // const thisSongInfo = {
    //   id: 1323832567,
    //   songsSheet: 'recomand'
    // }

    var appInst = getApp()
    let songsInfo = appInst.globalData.songsInfo

    if (songsInfo.songId == thisSongInfo.id) {
      // 还是那首，恢复状态
      this.setData({
        id: songsInfo.songId,
        isPlay: songsInfo.isPlay
      })
    } else {
      this.setData({
        id: thisSongInfo.id
      })
    }

    // 获取歌曲信息
    this.getSongInfo()
    // 获取歌单
    this.getSongsSheet(thisSongInfo.songsSheet)

    // BackgroundAudioManager设置
    this.backgroundAudioManager = wx.getBackgroundAudioManager()
    this.backgroundAudioManager.onEnded(() => {
      if (this.data.playMode === 'list') {
        this.handleNext()
      }
    })
    this.backgroundAudioManager.onTimeUpdate(() => {
      if (this.data.isPlay) {
        let curtime = this.backgroundAudioManager.currentTime * 1000
        let proc = Math.floor((this.backgroundAudioManager.currentTime / this.backgroundAudioManager.duration) * 530)
        this.setData({
          currenTime: moment(curtime).format('mm:ss'),
          proc
        })
      }
    })
  },
  // 获取歌曲信息
  async getSongInfo() {
    let res = await request('/song/detail', {
      ids: this.data.id
    })
    let url = await request('/song/url', {
      id: this.data.id
    })
    this.setData({
      songInfo: res.songs[0],
      url: url.data[0].url,
      durationTime: moment(res.songs[0].dt).format('mm:ss')
    })
    // 更改标题
    wx.setNavigationBarTitle({
      title: this.data.songInfo.name
    })
  },
  // 获取歌单信息
  async getSongsSheet(songsSheet) {
    let index = -1
    if (songsSheet === 'recomand') {
      const { data: res } = await request('/recommend/songs')
      index = res.dailySongs.findIndex(item => item.id == this.data.id)
      this.setData({
        songsSheet: res.dailySongs,
        index
      })
    }
  },
  // 播放或暂停
  handleCtrl() {
    var appInst = getApp()

    if (this.data.isPlay) {
      this.setData({
        isPlay: false
      })
      wx.pauseBackgroundAudio()
    } else {
      this.setData({
        isPlay: true
      })
      wx.playBackgroundAudio({
        dataUrl: this.data.url
      })
    }

    appInst.globalData.songsInfo.songId = this.data.id
    appInst.globalData.songsInfo.isPlay = this.data.isPlay
  },
  // 上下一首
  async changeSong(index) {
    //* 0 首先必然是关闭状态
    const isPlay = this.data.isPlay
    this.setData({
      currenTime: '00:00',
      proc: 0,
      isPlay: false
    })
    // wx.stopBackgroundAudio()
    //* 1 获取新歌曲信息
    this.setData({
      index,
      id: this.data.songsSheet[index].id
    })
    // 获取歌曲信息
    await this.getSongInfo()
    console.log(this.data.url)
    //* 2 根据刚才的状态决定现在的状态
    if (isPlay) {
      this.setData({
        isPlay
      })
      wx.playBackgroundAudio({
        dataUrl: this.data.url
      })
    }
  },
  //
  handleLast() {
    console.log('last')
    let index = this.data.index
    index--
    if (index < 0) index = this.data.songsSheet.length - 1
    this.changeSong(index)
  },
  //
  handleNext() {
    let index = this.data.index
    index++
    if (index >= this.data.songsSheet.length) index = 0
    this.changeSong(index)
  },
  // 显示歌单
  handelGeDan() {
    this.setData({
      isSheetShow: true
    })
  },
  handleClose() {
    if (this.data.isSheetShow) {
      this.setData({
        isSheetShow: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
})
